package com.yuxi.controller;

import com.yuxi.model.Red;
import com.yuxi.service.RedService;
import com.yuxi.service.RedpackService;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeoutException;

/**
 * Created by yuxi on 16/11/7.
 */
@RestController
@RequestMapping("/red")
public class RedController {
    Log log = LogFactory.getLog(RedController.class);
    @Autowired
    private RedpackService redpackService;
    @Autowired
    private MemcachedClient memcachedClient;
    @Autowired
    private RedService redService;
    final int count = 2000;


    //    @ApiOperation("update")
//    @ApiImplicitParams({
//    })
//    @ApiResponses({
//            @ApiResponse(code = 400, message = "请求参数没填好"),
//            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
//    })
    @RequestMapping(value = "/update")
    public Object update() {
//        List<MyThread12> myThreadList = new ArrayList<>();
//        for (int i = 1; i <= count; i++) {
//            MyThread12 myThread1 = new MyThread12();
//            myThreadList.add(myThread1);
//        }
//        long start = System.currentTimeMillis();
//        for (int i=0; i<myThreadList.size() ; i++) {
//            new Thread(myThreadList.get(i),"name"+i).start();
//        }
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);
//        return (end - start) + "";
        try {
            memcachedClient.set("key", 0, String.valueOf(10000));
            memcachedClient.delete("key");
            Object o  = memcachedClient.get("key");
            return o;
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MemcachedException e) {
            e.printStackTrace();
        }
        return null;
    }

    class MyThread12 implements Runnable {

        @Override
        public void run() {
            try {
                //缓存自增
                int id = new Random().nextInt();
                long l = memcachedClient.incr("three", 1, 1);
                redService.update(new Red((int) l, id, id));
            } catch (TimeoutException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (MemcachedException e) {
                e.printStackTrace();
            }

        }
    }


    //    @ApiOperation("insert")
//    @ApiImplicitParams({
//    })
//    @ApiResponses({
//            @ApiResponse(code = 400, message = "请求参数没填好"),
//            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
//    })
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Object insert() {
        List<MyThread> myThreadList = new ArrayList<>();
        for (int i = 1; i <= count; i++) {
            MyThread myThread1 = new MyThread();
            myThreadList.add(myThread1);
        }
        long start = System.currentTimeMillis();
        for (int i = 0; i < myThreadList.size(); i++) {
            new Thread(myThreadList.get(i), "name" + i).start();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        return (end - start) + "";
    }

    class MyThread implements Runnable {

        @Override
        public void run() {
            int id = new Random().nextInt();
            redService.insert(new Red(id, id));
        }
    }
}
